
import java.sql.*;   // methods to process SQL commands
import java.util.ArrayList;
import java.util.Collections;

import javax.sql.*;  // ditto
//import org.postgresql.jdbc3.Jdbc3PoolingDataSource; // connection via JDBC to postgrSQL
import java.io.IOException;   // enable IOException objects & handling
import java.io.PrintWriter;   // enable PrintWriter (so can use .println())
import javax.servlet.http.*;  // provides servlet functionality
import javax.servlet.*;	      // ditto
import javax.*;


public class BrowseByGenres extends HttpServlet
{
	//private static DataSource ds;	// contains DB and connection data; see init()
	private static Connection connection;
	//private static StringBuffer buf;
	//private static ArrayList<String> genres;
	private static int numberOfRows;
	
	public static String issueQuery()
    {	
		numberOfRows = 1;
		ArrayList<String>  genres = new ArrayList<String>();
		StringBuffer buf = new StringBuffer();
		// see the API doc at java.sun.com for details
		try
		{
			// connect to DB
			Class.forName("org.postgresql.Driver").newInstance();
			connection = DriverManager.getConnection(
					"jdbc:postgresql://localhost/fabflixs", "testuser", "testpass");
			
			// get and build string of test DB's metadata
			Statement select = connection.createStatement();
			ResultSet result = select.executeQuery("Select * from genres");
			
			// add table's contents to buffered string
			
			
			while (result.next())
			{
				String tempGenre = result.getString(2);
				
				if(!genres.contains(tempGenre.toLowerCase()))	
					genres.add(tempGenre.toLowerCase());
		
			}
			
			Collections.sort(genres);
			
			buf.append("<br> <b>There are " + genres.size() + " different genres.</b> <br>");
			
			buf.append("<TR valign=top>");
			int counterTR = 1;
			int counterTRclose = 0;
			for(int i = 0; i < genres.size(); i++)
			{
				buf.append("<TD valign=top align=center width=220 height=40><div class=\"wpmd\">");
				buf.append("<div>");
				buf.append("<input type=\"radio\" name=\"genres\" value=\"" + genres.get(i) + "\"> " + genres.get(i));
				buf.append("</div>");
				buf.append("</TD>");
				
				if((i+1)%4 == 0)
				{
					counterTRclose = 1;
					buf.append("</TR>");
					buf.append("<TR valign=top>");	
					numberOfRows++;
				}
			}
			
			if(counterTR != counterTRclose)
				buf.append("</TR>");
			// query sent; close connection to DB
			connection.close();
		}
		catch (SQLException sqle)
		{
			buf.append("SQL exception occured");
		}	
		catch (ClassNotFoundException cnfe)
		{
			buf.append("ClassNotFound exception occured");
		}
		catch (InstantiationException ie)
		{
			buf.append("Instantiation exception occured");
		}
		catch (IllegalAccessException iae)
		{
			buf.append("IllegalAccess exception occured");
		}
		
		// return buffered string of text
		return buf.toString();
	}
	
	
	
	// Print HTML for Web page heading
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		StringBuffer buf = new StringBuffer();
		
		if (!Bridge.hasBeenToMainPage(request.getSession(), response.getWriter()))
		{
			return;
		}

		//creating form
		buf.append("<form name=\"genreForm\" action=\"FindMoviesWithGenres\" method=\"GET\">");

		
		//making table

		buf.append("<div><TABLE bgcolor=\"#FFFFFF\" border=0 bordercolorlight=\"#C0C0C0\" bordercolordark=\"#808080\">");
		
		buf.append(issueQuery());
		
		buf.append("<TR valign=top>");
		buf.append("<TD align=center colspan=4>");
		buf.append("<div id=\"enter\" \"><input type=\"image\" name=\"EnterButton\" src=\"../images/Enter.png\"></div>");
		buf.append("</TD>");
		buf.append("</TR>");
		
		buf.append("</TABLE>");
		buf.append("</div>");
		
		buf.append("</div>"); //close off template
		//buf.append("<div id=\"buttonEnd\" style=\"position:absolute; overflow:hidden; left:"+left+"px; top:"+top+"px; \">");
	
		//buf.append("</div>");

		
		buf.append("</form>");
		out.println(Template.getTemplateHTML("Browse By Genres ", buf.toString(), Constants.straightToCart,
				request.getSession() ));
	}
	
	// Upon init of servlet, open the moviedb database
	public void init() 
	{
	}

	
}
